IF (NOT USE_OPENCV)
	SET(USE_OPENCV 0 CACHE BOOL "Use OpenCV (needed for camera calibration module)")
ENDIF()

IF (USE_OPENCV)
	# Try OpenCV 4 first.
	PKG_CHECK_MODULES(OPENCV4 opencv4>=4.0.0)
	IF (OPENCV4_FOUND)
		SET(CALIB_INCDIRS ${CAER_INCDIRS} ${OPENCV4_INCLUDE_DIRS})
		SET(CALIB_LIBDIRS ${CAER_LIBDIRS} ${OPENCV4_LIBRARY_DIRS})
		SET(CALIB_LIBS ${CAER_LIBS} ${OPENCV4_LIBRARIES})
	ELSE()
		# Else require OpenCV 3.1 at least.
		PKG_CHECK_MODULES(OPENCV3 REQUIRED opencv>=3.1.0)

		SET(CALIB_INCDIRS ${CAER_INCDIRS} ${OPENCV3_INCLUDE_DIRS})
		SET(CALIB_LIBDIRS ${CAER_LIBDIRS} ${OPENCV3_LIBRARY_DIRS})
		SET(CALIB_LIBS ${CAER_LIBS} ${OPENCV3_LIBRARIES})
	ENDIF()

	INCLUDE_DIRECTORIES(${CALIB_INCDIRS})
	LINK_DIRECTORIES(${CALIB_LIBDIRS})

	ADD_DEFINITIONS(-DLIBCAER_FRAMECPP_OPENCV_INSTALLED=1)

	ADD_LIBRARY(cameracalibration SHARED cameracalibration.c calibration.cpp calibration_wrapper.cpp)

	SET_TARGET_PROPERTIES(cameracalibration
		PROPERTIES
		PREFIX "caer_"
	)

	TARGET_LINK_LIBRARIES(cameracalibration ${CALIB_LIBS})

	INSTALL(TARGETS cameracalibration DESTINATION ${CAER_MODULES_DIR})
ENDIF()
